package com.itboye.ihomebank.adapter;

import java.util.Iterator;
import java.util.List;
import java.util.Observable;
import java.util.Observer;

import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;

import com.itboye.ihomebank.R;
import com.itboye.ihomebank.bean.ChanKanWeiXiuBean;
import com.itboye.ihomebank.bean.ResultEntity;
import com.itboye.ihomebank.constants.SPContants;
import com.itboye.ihomebank.me.ActivityPingJiaWorker;
import com.itboye.ihomebank.me.ActivityXiuLiZhiFu;
import com.itboye.ihomebank.presenter.RepairPersenter;
import com.itboye.ihomebank.support.commonadapter.MutiplyCommonAdapter;
import com.itboye.ihomebank.support.commonadapter.ViewHolder;
import com.itboye.ihomebank.util.ByAlert;
import com.itboye.ihomebank.util.SPUtils;

/**
 * 我的生活管家（待评价）
 * 
 */
public class MyGuanJiaAdapter04 extends MutiplyCommonAdapter<ChanKanWeiXiuBean> implements Observer {
	List<ChanKanWeiXiuBean> datas;
	Context context;
	protected String id;
	protected String money;
	protected String repairStatus;
	RepairPersenter repairPersenter;
	protected String uid;
	public MyGuanJiaAdapter04(Context context, List<ChanKanWeiXiuBean> datas,
			int... layoutId) {
		super(context, datas, layoutId);
		this.datas = datas;
		this.context = context;
		repairPersenter=new RepairPersenter(this);
		uid = SPUtils.get(context, null, SPContants.USER_ID, "") + "";
	}

	@Override
	public int getItemViewType(int position) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public void getItemPosition(int position) {
		// TODO Auto-generated method stub

	}

	@Override
	public void convert(ViewHolder holder, final ChanKanWeiXiuBean t, int type,
			int position) {
		holder.setText(R.id.guanjia_danhao, t.getId());
		holder.setText(R.id.guanjia_leixing, t.getRepairstatus());
		holder.setText(R.id.guanjia_address, t.getAddress());
		holder.setText(R.id.guanjia_name, t.getUname());
//		if (t.getPayStatus().equals("0")) {
			holder.setText(R.id.guanjia_01, "已完成");
//		} else {
//			holder.setText(R.id.guanjia_01, "评价师傅");
//		}
		holder.setVisible(R.id.guanjia_02, false);
		holder.setVisible(R.id.guanjia_03, false);

//		holder.setOnClickListener(R.id.guanjia_01, position,
//				new OnClickListener() {
//
//					@Override
//					public void onClick(View v) {
//						TextView text = (TextView) v;
//						if (text.getText().toString().equals("去支付")) {
//							id = t.getId();
//							money = t.getMoney();
//							repairStatus = t.getRepairstatus();
////							repairPersenter.xiuLiEnd(t.getId(), uid);
//							Intent intent = new Intent(context,
//									ActivityXiuLiZhiFu.class);
//							intent.putExtra("id", id);
//							intent.putExtra("money", money);
//							intent.putExtra("type", repairStatus);
//							context.startActivity(intent);
//						} else {
//							Intent intent = new Intent(context,
//									ActivityPingJiaWorker.class);
//							intent.putExtra("head", t.getHead());
//							intent.putExtra("name", t.getWname());
//							intent.putExtra("phone", t.getWmobile());
//							intent.putExtra("wid", t.getWid());
//							intent.putExtra("id", t.getId());
//							context.startActivity(intent);
//						}
//
//					}
//				});

	}

	@Override
	public void update(Observable observable, Object data) {
		// TODO Auto-generated method stub
		ResultEntity entity = handlerError(data);
		// ByAlert.alert(entity.getData());
		if (entity != null) {
			if (!entity.getCode().equals(0)) {

				if (entity.getEventType() == RepairPersenter.theEnd_success) {
					ByAlert.alert(entity.getData());

					Iterator<ChanKanWeiXiuBean> iterator = datas.iterator();
					while (iterator.hasNext()) {
						ChanKanWeiXiuBean bean = iterator.next();
						if (id.equals(bean.getId())) {
							iterator.remove();
							notifyDataSetChanged();
							break;
						}
					}
					Intent intent = new Intent(context,
							ActivityXiuLiZhiFu.class);
					intent.putExtra("id", id);
					intent.putExtra("money", money);
					intent.putExtra("type", repairStatus);
					context.startActivity(intent);
				}
				if (entity.getEventType() == RepairPersenter.theEnd_fail) {
					ByAlert.alert(entity.getData());
				}
			}
		}
	}

}
